var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var goTop = require('../widget/goTop/goTop');

Array.prototype.contains = function (obj) {
  var i = this.length;
  while (i--) {
    if (this[i] == obj) {
      return true;
    }
  }
  return false;
}

var pageSetting = {
  data: {
    pageShow: 0,
    loading: 0,
    refundModalShow: 0,//是否展示退款框
    reasonIndex: -1,    
    reasonArr: ['不想要了/下错单', '操作有误（地址、商品写错）', '货品质量问题／过了生产日期', '快递一直未到，不能按时送达', '未按照约定时间发货', '货送错了／量送少了', '其他原因']
  },

  onLoad: function (option) {
    this.loadPageData(option);
  },
  loadPageData: function (option) {
    var that = this;
    var setData = {};
    setData.order_id = that.data.order_id = option.order_id;
    var data = {
      "zp:zc_api:order:detail":
      { "order_id": option.order_id }
    };
    modal.loading();
    //获取订单信息
    request.post({
      path: '/wxapp_api/api'
    }, data).then(function (res) {
      modal.hide();
      if (res['zp:zc_api:order:detail'].ret == 0) {
        var respData = res['zp:zc_api:order:detail'].data;        
        respData.isRed = [9, 3, 12, 8].contains(respData.status);//订单是否标红
        respData.image = (JSON.parse(respData.act_img_url))[0];
        respData.phoneArr = respData.service_phone.split("/");
        setData.orderData = that.data.orderData = respData;
        setData.pageShow = that.data.pageShow = 1;
        that.setData(setData);
      } else {
        modal.toast(res['zp:zc_api:order:detail']['msg']);
      }
    });
  },
  //申请售后
  serviceLayerShow: function () {
    var that = this;
    modal.confirm('提示', '确定拨打客服电话：' + that.data.orderData.phoneArr[0] + '吗？', function (res) {
      if (res) {
        wx.makePhoneCall({
          phoneNumber: that.data.orderData.service_phone
        });
      }
    });
  },
  //确认收货
  receive: function () {
    var that = this;
    if (that.data.loading == 0) {
      var data = {
        "zp:zc_api:order:receive": {
          "id": that.data.order_id
        }
      };
    }
    modal.loading();
    that.data.loading = 1;
    //获取订单信息
    request.post({
      path: '/wxapp_api/api'
    }, data).then(function (res) {
      modal.hide();
      if (res['zp:zc_api:order:receive'].ret == 0) {
        that.data.orderData.status = '4';
        that.data.orderData.status_desc = '已收货';
        app.globalData.listOrderState = true;
        that.setData({
          orderData: that.data.orderData
        });        
      } else {
        modal.toast(res['zp:zc_api:order:receive']['msg']);
      }
      that.data.loading = 0;
    });
  },
  bindPickerChange: function (ev) {
    this.data.reasonIndex = ev.detail.value;
    this.setData({
      reasonIndex: this.data.reasonIndex
    })
  },
  showRefundModal: function () {
    this.data.refundModalShow = 1;
    this.setData({
      refundModalShow: this.data.refundModalShow
    })
  },
  hideRefundModal: function () {
    this.data.refundModalShow = 0;
    this.setData({
      refundModalShow: this.data.refundModalShow
    })
  },
  submitRefund: function () {
    var that = this;
    if (that.data.reasonIndex > -1 && that.data.reasonIndex < that.data.reasonArr.length) {
      if (that.data.loading == 0) {
        var data = {
          "zp:zc_api:order:refund": {
            "id": that.data.order_id,
            "reason": that.data.reasonArr[that.data.reasonIndex]
          }
        };
      }
      modal.loading();
      that.data.loading = 1;
      //获取订单信息
      request.post({
        path: '/wxapp_api/api'
      }, data).then(function (res) {
        modal.hide();
        if (res['zp:zc_api:order:refund'].ret == 0) {
          that.data.orderData.status = 12;
          that.data.orderData.status_desc = '申请退款中';
          that.data.refundModalShow = 0;
          app.globalData.listOrderState = true;
          that.setData({
            orderData: that.data.orderData,
            refundModalShow: that.data.refundModalShow
          });
        } else {
          modal.toast(res['zp:zc_api:order:refund']['msg']);
        }
        that.data.loading = 0;
      });
    } else {
      modal.toast('请选择退款原因');
    }
  }
}
Page(pageSetting)